import $store from '@/store/index.js';
export default {
	/* 获取申请列表 */
	getFrindAppList() {
		return new Promise((resolve, reject) => {
			// 然后执行join操作来联查friendApp与userinfo表
			$store.state.user.sqlite.friendApp
				.fields('friendApp.friend_id, friendApp.create_time,userinfo.nick_name, userinfo.avatar_url')
				.join('userinfo', 'friend_id', 'user_id')
				.where({ field: 'friendApp.isMe', val: `'0'` })
				.get() // 执行查询
				.then(results => {
					resolve(results)
				})
				.catch(error => {
					reject(error)
				});
		})
	},
	//更新或添加好友申请
	async addOrEditFrindAppInfo(data) {
		//逻辑是先插入如果插入失败，就更新
		try{
			await $store.state.user.sqlite.friendApp.create(data)
			return true
		}catch(e){
			try{
				await this.editFrindAppInfo(data.apply_id,data)
				return true
			}catch(e){
				console.log(e);
				throw e
			}
		}
	},
	//获取好友申请
	getFrindAppInfo(apply_id) {
		return new Promise((resolve, reject) => {
			$store.state.user.sqlite.friendApp.where({ field: 'apply_id', val: `'${apply_id}'` }).first()
				.then(results => {
					resolve(results)
				})
				.catch(error => {
					reject(error)
				});
		})
	},
	//更新好友申请
	editFrindAppInfo(apply_id,data) {
		return $store.state.user.sqlite.friendApp.where({ field: 'apply_id', val: `'${apply_id}'`}).update(data)
	},
	//删除
	delFrindAppInfo(apply_id) {
		return $store.state.user.sqlite.friendApp.where({ field: 'apply_id', val: `'${apply_id}'`}).delete()
	}
}